package com.example.demo.controller;

import com.example.demo.entity.Department;
import com.example.demo.repository.DepartmentRepository;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@AllArgsConstructor
@RequestMapping("/departs")
@RestController
public class DepartmentController {

    @Resource
    private final DepartmentRepository departmentRepository;

    @GetMapping("")
    public List<Department> departs() {
        return departmentRepository.findAll();
    }

    @GetMapping("/{id}")
    public Department getDepart(@PathVariable Integer id) {
        return departmentRepository.findById(id).orElse(new Department());
    }

    @PostMapping("")
    public Department newDepart(@RequestBody Department newDepart) {
        return departmentRepository.save(newDepart);
    }
}
